#!/bin/bash

# 判断操作系统类型，选择 wget 命令
if [[ "$(uname)" == "Linux" || "$(uname)" == "Darwin" ]]; then
    WGET_CMD="wget"
else
    WGET_CMD="./wget"
fi

# 并发数量
THREADS=12

# 临时任务队列
queue_file="download_jobs.queue"
> "$queue_file"

# 构造任务列表：每个任务一行包含 url|filepath|remote_size
for list_file in *.txt; do
    echo "📄 处理列表文件: $list_file"
    while IFS= read -r url; do
        url=$(echo "$url" | tr -d '\r')
        [[ -z "$url" ]] && continue

        filename=$(basename "$url" | tr -d '\r')
        datetime=$(echo "$filename" | grep -oP '\d{14}' | head -n1)

        if [[ -z "$datetime" ]]; then
            echo "⚠️ 无法从文件名中提取时间: $filename"
            continue
        fi

        year=${datetime:0:4}
        month=${datetime:4:2}
        day=${datetime:6:2}
        hour=${datetime:8:2}
        target_dir="$year/$month/$day/$hour"
        mkdir -p "$target_dir"
        filepath="$target_dir/$filename"

        echo "$url|$filepath" >> "$queue_file"
    done < "$list_file"
done

# 定义下载函数
download_file() {
    IFS='|' read -r url filepath <<< "$1"
    filename=$(basename "$filepath")

    # 模拟远程文件大小（建议实际情况中用 curl 获取）
    remote_size=8000000

    if [[ -f "$filepath" ]]; then
        local_size=$(stat -c %s "$filepath")
        if [[ "$local_size" -ge "$remote_size" ]]; then
            echo "✅ 跳过：已完整下载 $filename"
            return
        else
            echo "🔁 检测到未完整，准备断点续传：$filename"
        fi
    fi

    "$WGET_CMD" -c "$url" -O "$filepath"
    if [[ $? -eq 0 ]]; then
        echo "✅ 下载完成: $filename"
    else
        echo "❌ 下载失败: $filename" >> fail.log
    fi
}

export -f download_file
export WGET_CMD

# 使用 xargs 并发执行
cat "$queue_file" | xargs -I {} -P "$THREADS" bash -c 'download_file "$@"' _ {}

